<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">问卷管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>问卷管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：<input id="paper-edit-search" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="paper-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="paper-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
        </div>

        <!-- 数据表格 -->
        <table class="layui-table" id="paper-table" lay-filter="paper-table"></table>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="paper-state">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="preview">预览</a>
    <a class="layui-btn layui-btn-xs" lay-event="ques">问题管理</a>
</script>
<script>
    layui.use(['form', 'table', 'config', 'admin'], function () {
        let table = layui.table;
        let config = layui.config;
        let layer = layui.layer;
        let admin = layui.admin;


        let url = config.base_server + 'api-paper/paper/getPapers';
        let params = {
            access_token: config.getToken().access_token
        };
        let cols = [[
            {type: 'numbers'},
            {field: 'papeid', sort: true, title: 'ID', width: 80},
            {field: 'ptitle', sort: true, title: '问卷标题'},
            {field: 'instru', sort: true, title: '问卷说明'},
            {field: 'publisStr', sort: true, title: '发布人'},
            {field: 'facegpStr', sort: true, title: '调查对象'},
            {field: 'rtcodeStr', sort: true, title: '评分类型'},
            {field: 'pasort', sort: true, title: '排序号', width: 80},
            {field: 'endtim', sort: true, title: '结束日期'},
            {field: 'cretim', sort: true, title: '创建时间'},
            {templet: '#paper-state', width: 270, align: 'center', title: '操作'}
        ]];

        // 渲染表格
        table.render({
            elem: '#paper-table',
            url: url,
            where: params,
            page: true,
            cols: cols
        });

        // 工具条点击事件
        table.on('tool(paper-table)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;

            if (layEvent === 'edit') {
                showEditModel(data);
            } else if (layEvent === 'del') {
                layer.confirm('此操作不可逆，确定删除吗？', function () {
                    layer.load(2);
                    admin.req('api-paper/paper/delete/' + obj.data.papeid, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code === 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            table.reload('paper-table', {});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'DELETE');

                });
            } else if (layEvent === 'preview') {
                previewPaper(obj.data.papeid);
            } else if (layEvent === 'ques') {
                showQuestionDialog(obj.data.papeid);
            }
        });

        // 添加按钮点击事件
        $('#paper-btn-add').click(function () {
            showEditModel();
        });

        //显示表单弹窗
        let showEditModel = function (data) {
            admin.putTempData('t_paper', data);
            let title = data ? '修改问卷' : '添加问卷';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/paper_form.html',
                area: '800px',
                offset: '200px',
                finish: function () {
                    table.reload('paper-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#paper-btn-search').click(function () {
            let key = $('#paper-edit-search').val();
            table.reload('paper-table', {where: {searchKey: key}});
        });

        // 问卷预览
        let previewPaper = function (papeid) {
            admin.putTempData('t_paperPreview', papeid);
            let title = '问卷预览';
            admin.popupCenter({
                title: title,
                path: 'pages/paper/paperPreview_form.html',
                area: '1200px',
                offset: '50px',
                finish: function () {
                    table.reload('question-table', {});
                }
            });
        };

        // 问题管理
        let showQuestionDialog = function (papeid) {
            layer.open({
                type: 1,
                title: '问题管理',
                area: ['600px', '500px'],
                offset: '120px',
                content: '<ul id="treeQuest" class="ztree" style="padding: 25px 0px 20px 60px;"></ul>',
                btn: ['保存', '关闭'],
                btnAlign: 'c',
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow-y', 'auto');
                    layer.load(2);
                    let setting = {
                        check: {enable: true},
                        data: {
                            simpleData: {
                                enable: true
                            }
                        }
                    };

                    admin.req('api-paper/question/' + papeid + '/getAllQuestions', {}, function (data) {
                        $.fn.zTree.init($('#treeQuest'), setting, data.data);
                        layer.closeAll('loading');
                    }, 'GET');

                },
                yes: function (index) {
                    layer.load(1);
                    let treeObj = $.fn.zTree.getZTreeObj('treeQuest');
                    let nodes = treeObj.getCheckedNodes(true);
                    let ids = [];
                    for (let i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    let data = {};
                    data.papeid = papeid;
                    data.questIds = ids;

                    admin.req('api-paper/question/addQuestions', JSON.stringify(data), function (data) {
                        layer.closeAll('loading');
                        if (0 === data.code) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                            layer.close(index);
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                }
            });
        }

    });
</script>